import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';
import 'package:get/get.dart';

import '../bluetooth/send_data.dart';
import '../search/search_view.dart';
import '../tool/assets.dart';
import '../tool/model.dart';

class BottomBar extends StatefulWidget {
  const BottomBar({super.key});

  @override
  State<BottomBar> createState() => _BottomBarState();
}

class _BottomBarState extends State<BottomBar> {
  final GlobalController modelController = Get.put(GlobalController());
  @override
  void initState() {
    super.initState();

    listenBluetooth(context);
  }

  void listenBluetooth(BuildContext context) {
    ///监听蓝牙外设连接状态
    BluetoothManager.instance.connectionStateStream.listen((value) {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
      return Container(
        padding: const EdgeInsets.only(top: 5, bottom: 15),
        child: SizedBox.expand(
          child: Stack(
            children: [
              ///蓝牙按钮
              Positioned(
                top: 10,
                left: 0,
                bottom: 10,
                width: constraints.maxHeight - 20,
                child: InkWell(
                  child: Image.asset(
                      BluetoothManager.instance.connectedDeviceList.isNotEmpty ? assets.ble_on : assets.ble_off),
                  onTap: () {
                    Size size = MediaQuery.of(context).size;
                    SearchView search = SearchView(height: size.height - 160);
                    search.show(context, completion: () {});
                  },
                ),
              ),

              ///右边
              Positioned(
                  top: 10,
                  bottom: 10,
                  right: 10,
                  left: constraints.maxHeight - 20,
                  child: Container(
                    padding: const EdgeInsets.only(left: 0, right: 0, top: 10, bottom: 10),
                    decoration: BoxDecoration(borderRadius: BorderRadius.circular(7), color: Color_Bottom_bg),
                    child: Obx(() {
                      return Row(
                        children: [
                          Image.asset(assets.batter, width: constraints.maxHeight - 30),
                          const Expanded(
                              child: Text(
                            '20% SOC Protection Switch',
                            style: TextStyle(fontSize: 14, color: Colors.white, height: 1.0),
                          )),
                          SizedBox(
                              width: constraints.maxHeight - 30,
                              child: Transform.scale(
                                scale: 0.8,
                                child: Switch(
                                    inactiveTrackColor: Color_Line_bg,
                                    trackOutlineWidth: const WidgetStatePropertyAll(0.1),
                                    value: modelController.defaultModel.value.openSoc,
                                    onChanged: (p) {
                                      modelController.updateDefault(($0) {
                                        $0.openSoc = p;
                                      });
                                      SendData.send('控制指令', [0x09, !p ? 0x01 : 0x00]);
                                    }),
                              )),
                        ],
                      );
                    }),
                  )),
            ],
          ),
        ),
      );
    });
  }
}
